package com.itye.deom1.view;

import com.itye.deom1.Student;
import com.itye.deom1.Group;
import com.itye.deom1.until.Constant;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import java.util.Random;
import java.util.Map;
import java.util.List;

public class RandomStudentPanel extends JPanel {
    private JLabel lbl2 = new JLabel("学生姓名：");
    private JLabel lbl3 = new JLabel("学生照片：");
    private JLabel lblPic = new JLabel("照片");
    private JTextField txtStudent = new JTextField();
    private JButton btnChooseStudent = new JButton("随机学生");
    private JButton btnAbsence = new JButton("缺勤");
    private JButton btnLeave = new JButton("请假");
    private JButton btnAnswer = new JButton("答题");
    private JButton btnScore = new JButton("小组评分");
    Thread threadStudent = null;   // 随机抽取学生的线程
    private volatile boolean isRunning = false;

    public RandomStudentPanel() {
        this.setBorder(new TitledBorder(new EtchedBorder(), "随机学生点名"));
        this.setLayout(null);
        this.add(lbl2);
        this.add(lbl3);
        this.add(txtStudent);
        this.add(lblPic);
        this.add(btnChooseStudent);
        this.add(btnAbsence);
        this.add(btnLeave);
        this.add(btnAnswer);
        this.add(btnScore);

        lbl2.setBounds(160, 50, 100, 30);
        txtStudent.setBounds(160, 90, 130, 30);
        txtStudent.setEditable(false);
        lblPic.setBounds(160, 130, 130, 150);
        btnChooseStudent.setBounds(160, 300, 130, 30);
        btnAbsence.setBounds(160, 340, 60, 30);
        btnLeave.setBounds(230, 340, 60, 30);
        btnAnswer.setBounds(300, 340, 60, 30);
        btnScore.setBounds(370, 340, 90, 30);

        // 实现随机学生功能
        btnChooseStudent.addActionListener(e -> {
            if (Constant.students.isEmpty()) {
                JOptionPane.showMessageDialog(this, "没有可选择的学生", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            if (e.getActionCommand().equals("停")) {
                isRunning = false;
                btnChooseStudent.setText("随机学生");
                if (threadStudent != null) {
                    threadStudent.interrupt();
                }
            } else {
                btnChooseStudent.setText("停");
                isRunning = true;
                threadStudent = new Thread(() -> {
                    Random random = new Random();
                    while (isRunning) {
                        try {
                            // 随机选择一个学生
                            int index = random.nextInt(Constant.students.size());
                            Student student = Constant.students.get(index);
                            txtStudent.setText(student.getName());
                            Thread.sleep(50);  // 控制滚动速度
                        } catch (InterruptedException ex) {
                            break;
                        }
                    }
                });
                threadStudent.start();
            }
        });

        // 缺勤按钮
        btnAbsence.addActionListener(e -> {
            if (txtStudent.getText() == null || txtStudent.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请先随机选择学生", "", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "已记录缺勤", "", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 请假按钮
        btnLeave.addActionListener(e -> {
            if (txtStudent.getText() == null || txtStudent.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请先随机选择学生", "", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "已记录请假", "", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 答题按钮
        btnAnswer.addActionListener(e -> {
            if (txtStudent.getText() == null || txtStudent.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请先随机选择学生", "", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, "回答正确", "", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 小组评分按钮事件
        btnScore.addActionListener(e -> {
            if (txtStudent.getText() == null || txtStudent.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this, "请先随机选择学生", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            // 查找学生所在的小组
            String studentName = txtStudent.getText();
            Group studentGroup = null;
            for (Map.Entry<Group, List<Student>> entry : Constant.groups.entrySet()) {
                for (Student student : entry.getValue()) {
                    if (student.getName().equals(studentName)) {
                        studentGroup = entry.getKey();
                        break;
                    }
                }
                if (studentGroup != null) break;
            }

            if (studentGroup == null) {
                JOptionPane.showMessageDialog(this, "未找到该学生所在的小组", "", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            // 创建评分对话框
            String score = JOptionPane.showInputDialog(this, 
                "请输入小组" + studentGroup.getGroupId() + "的分数（0-100）:", 
                "小组评分", 
                JOptionPane.QUESTION_MESSAGE);

            // 验证分数
            if (score != null && !score.isEmpty()) {
                try {
                    int scoreNum = Integer.parseInt(score);
                    if (scoreNum >= 0 && scoreNum <= 100) {
                        // TODO: 保存小组分数
                        JOptionPane.showMessageDialog(this, 
                            "已为小组" + studentGroup.getGroupId() + "评分：" + scoreNum + "分", 
                            "评分成功", 
                            JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(this, 
                            "分数必须在0-100之间", 
                            "评分错误", 
                            JOptionPane.ERROR_MESSAGE);
                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, 
                        "请输入有效的数字", 
                        "评分错误", 
                        JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }
}
